/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hhf.flex.ascreator.patterns;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 *
 * @author utilisateur
 */
public class ASGettersPattern {

    /**
     * Ecrit le corps de la fonction send pour un Getter. (méthode qui appelle
     * le service distant)
     * @param out
     * @param arguments
     * @param className
     * @param acceptOutOfDate
     * @throws IOException 
     */
    public static void writeGetterSendBody(OutputStreamWriter out, String arguments, String className, boolean acceptOutOfDate, List<String> cancelFunctionsNames, List<String> chainLoadFunctionsNames) throws IOException {
        out.write("\t\t\t//GETTER SEND BODY\n");
        out.write("\t\t\tCONFIG::air {\n");
        out.write("\t\t\t\tif(NetworkMonitor.available) {\n");
        out.write("\t\t\t\t\ttry {\n");
        out.write("\t\t\t\t\t\tvar srvid:String = getCacheServiceId(" + arguments + ");\n");
        out.write("\t\t\t\t\t\tvar cacheService:" + className + "CacheService = new " + className + "CacheService();\n");
        out.write("\t\t\t\t\t\tcacheService.id = srvid;\n");
        out.write("\t\t\t\t\t\tem.remove(cacheService);\n");
        out.write("\t\t\t\t\t} catch(e:EntityNotFoundException) {}\n");
        out.write("\t\t\t\t\taddNetworkHandlers(); // rajout des handlers réseau\n");
        out.write("\t\t\t\t\tthis.callDatasFromNetwork(" + arguments + "); // appelle du service distant\n");
        out.write("\t\t\t\t}\n");
        out.write("\t\t\t\telse{\n");
        if (acceptOutOfDate) {
            out.write("\t\t\t\t\ttry {\n");
            out.write("\t\t\t\t\t\tthis.datas = this.getDatasFromDB(" + arguments + ");\n");
            out.write("\t\t\t\t\t} catch(e:EntityNotFoundException) {\n");
            out.write("\t\t\t\t\t\tvar fault:Fault = new Fault(\"faultCode\", \"faultString\", \"faultDetail\");\n");
            out.write("\t\t\t\t\t\tvar event:FaultEvent = new FaultEvent(FaultEvent.FAULT, true, true, fault);\n");
            out.write("\t\t\t\t\t\tthis.dispatchEvent(event);\n");
            out.write("\t\t\t\t\t}\n");

        } else {
            out.write("\t\t\t\t\tvar isOutOfDate:Boolean = isDatasOutOfDate(" + arguments + ");\n");
            out.write("\t\t\t\t\tif(!isOutOfDate) { // on verifie la validité de la donnée en base\n");
            out.write("\t\t\t\t\t\ttry {\n");
            out.write("\t\t\t\t\t\t\tthis.datas = this.getDatasFromDB(" + arguments + ");\n");
            out.write("\t\t\t\t\t\t} catch(e:EntityNotFoundException) {\n");
            out.write("\t\t\t\t\t\t\tvar fault:Fault = new Fault(\"faultCode\", \"faultString\", \"faultDetail\");\n");
            out.write("\t\t\t\t\t\t\tvar event:FaultEvent = new FaultEvent(FaultEvent.FAULT, true, true, fault);\n");
            out.write("\t\t\t\t\t\t\tthis.dispatchEvent(event);\n");
            out.write("\t\t\t\t\t\t}\n");
            out.write("\t\t\t\t\t}\n");
        }

        out.write("\t\t\t\t}\n");
        out.write("\t\t\t}\n");
        out.write("\t\t\tCONFIG::flex {\n");
        out.write("\t\t\t\tif(NetworkMonitor.available) { // si le réseau est disponible on appelle le service distant\n");
        out.write("\t\t\t\t\taddNetworkHandlers(); // rajout des handlers réseau\n");
        out.write("\t\t\t\t\tthis.callDatasFromNetwork(" + arguments + "); // appelle du service distant\n");
        out.write("\t\t\t}\n");

        out.write("\t\t}\n");
        for (String cancelFunctionName : cancelFunctionsNames) {
            out.write("\t\t\t\tcancel_" + cancelFunctionName + "(" + arguments + ");\n");
        }
        for (String chainLoadFunctionName : chainLoadFunctionsNames) {
            out.write("\t\t\t\t" + chainLoadFunctionName + "(this.datas);\n");
        }
    }

    /**
     * Ecrit le corps de la fonction send pour un ReferenceGetter. (méthode qui 
     * appelle le service distant)
     * @param out
     * @param arguments
     * @param className
     * @param acceptOutOfDate
     * @param returnType
     * @throws IOException 
     */
    public static void writeReferenceGetterSendBody(OutputStreamWriter out, String arguments, String className, boolean acceptOutOfDate, String returnType, List<String> cancelFunctionsNames, List<String> chainLoadFunctionsNames) throws IOException {
        out.write("\t\t\t//REFERENCE GETTER SEND BODY\n");
        out.write("\t\t\tCONFIG::air {\n");
        out.write("\t\t\t\t// c'est un referentiel\n");
        out.write("\t\t\t\tvar isOutOfDate:Boolean = isDatasOutOfDate(" + arguments + ");\n");
        out.write("\t\t\t\tif(!isOutOfDate) { // on verifie la validité de la donnée en base\n");
        out.write("\t\t\t\t\ttry {\n");
        out.write("\t\t\t\t\t\tthis.datas = this.getDatasFromDB(" + arguments + ");\n");
        out.write("\t\t\t\t\t} catch(e:EntityNotFoundException) {\n");
        out.write("\t\t\t\t\t\taddNetworkHandlers(); // rajout des handlers réseau\n");
        out.write("\t\t\t\t\t\tthis.callDatasFromNetwork(" + arguments + "); // appelle du service distant\n");
        out.write("\t\t\t\t\t}\n");
        out.write("\t\t\t\t} else if(NetworkMonitor.available) { // si la donné est périmée et que le réseau est disponible on appelle le service distant\n");
        out.write("\t\t\t\t\ttry {\n");
        out.write("\t\t\t\t\t\tvar srvid:String = getCacheServiceId(" + arguments + ");\n");
        out.write("\t\t\t\t\t\tvar cacheService:" + className + "CacheService = new " + className + "CacheService();\n");
        out.write("\t\t\t\t\t\tcacheService.id = srvid;\n");
        out.write("\t\t\t\t\t\tem.remove(cacheService);\n");
        out.write("\t\t\t\t\t} catch(e:EntityNotFoundException) {}\n");
        out.write("\t\t\t\t\taddNetworkHandlers(); // rajout des handlers réseau\n");
        out.write("\t\t\t\t\tthis.callDatasFromNetwork(" + arguments + "); // appelle du service distant\n");
        out.write("\t\t\t\t} else { // la donnée est périmée et le réseau indisponible.\n");

        //données locales périmées, reseau indispo


        if (acceptOutOfDate) {
            out.write("\t\t\t\t\tvar datasDB:" + returnType + " = this.getDatasFromDB(" + arguments + "); // on récupere néanmoins les données périmées\n");
            out.write("\t\t\t\t\tif(datasDB) this.datas = datasDB;\n");
            out.write("\t\t\t\t\telse { // si il n'y en a pas, on léve une erreur.\n");
            out.write("\t\t\t\t\t\tvar fault:Fault = new Fault(\"faultCode\", \"faultString\", \"faultDetail\");\n");
            out.write("\t\t\t\t\t\tvar event:FaultEvent = new FaultEvent(FaultEvent.FAULT, true, true, fault);\n");
            out.write("\t\t\t\t\t\tthis.dispatchEvent(event);\n");
            out.write("\t\t\t\t\t}\n");
        } else {
            out.write("\t\t\t\t\tvar fault:Fault = new Fault(\"faultCode\", \"faultString\", \"faultDetail\");\n");
            out.write("\t\t\t\t\tvar event:FaultEvent = new FaultEvent(FaultEvent.FAULT, true, true, fault);\n");
            out.write("\t\t\t\t\tthis.dispatchEvent(event);\n");
        }

        out.write("\t\t\t\t}\n");
        out.write("\t\t\t}\n");
        out.write("\t\t\tCONFIG::flex {\n");
        out.write("\t\t\tif(NetworkMonitor.available) { // si le réseau est disponible on appelle le service distant\n");
        out.write("\t\t\t\taddNetworkHandlers(); // rajout des handlers réseau\n");
        out.write("\t\t\t\tthis.callDatasFromNetwork(" + arguments + "); // appelle du service distant\n");
        out.write("\t\t\t}\n");

        out.write("\t\t\t}\n");
        for (String cancelFunctionName : cancelFunctionsNames) {
            out.write("\t\t\t\tcancel_" + cancelFunctionName + "(" + arguments + ");\n");
        }
        for (String chainLoadFunctionName : chainLoadFunctionsNames) {
            out.write("\t\t\t\t" + chainLoadFunctionName + "(this.datas);\n");
        }
    }
}
